<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/4/24
 * Time: 15:46
 */

namespace Biz\module\Classes\adm;

use Biz\model\trade\OrderPay as mOrderPay;
use Biz\pockets\excel\DownExcel;

class RegisterExcel
{
    /**
     * 导出活动报名的数据
     * @param $title
     * @param $downList
     * @param $type
     * @return bool
     */
    public static function downList($title, $downList, $type)
    {
        if(!empty($downList)) {
            if($type == 1) {
                $result = self::downListByGoods($title, $downList);
            } else {
                $result = self::downListBySku($title, $downList);
            }
            return $result;
        }
        return false;
    }

    private static function downListBySku($title, $downList)
    {
        $header = ['商家uid', '商家名称', '商品id', '商品名称', 'skuId', 'sku名称', '原价', '活动价', '折扣', '库存', '加购数', '收藏数', '销量', '一级分类', '二级分类', '首次上架时间', '最近参加的活动', '已入选的活动'];
        $letter = range('A', 'R');

        $excelObj = new DownExcel();
        $excelObj->setFileName($title);
        $excelObj->setSheetTitle($title);
        //设置列宽
        $widths = ['A' => 20, 'B' => 20, 'C' => 20, 'D' => 50, 'E' => 20, 'F' => 20, 'G' => 15, 'H' => 15, 'I' => 10, 'J' => 10, 'K' => 10, 'L' => 10, 'M' => 10, 'N' => 20, 'O' => 20, 'P' => 30, 'Q' => 30, 'R' => 30];
        $excelObj->setWidth($widths);
        //默认单元格居中
        $excelObj->setDefaultStyleAlignment();
        foreach ($header as $key => $value) {
            //设置字体
            $excelObj->setFrontName($letter[$key]);
            //设置加粗
//                $excelObj->setFrontBold($letter[$key]);
            //设置大小
            $excelObj->setFrontSize($letter[$key]);
        }

        $excelObj->create($downList, $header);
        return true;
    }

    private static function downListByGoods($title, $downList)
    {
        $header = ['商家uid', '商家名称', '商品id', '商品名称', '一级分类', '二级分类', '商品售价(最低)', '商品售价(最高)', '活动价(最低)', '活动价(最高)', '折扣比(最低)', '折扣比(最高)', '总库存', '加购', '收藏', '销量', '首次上架时间', '最近参加的活动', '已入选的活动'];
        $letter = range('A', 'S');
        $excelObj = new DownExcel();
        $excelObj->setFileName($title);
        $excelObj->setSheetTitle($title);
        //设置列宽
        $widths = ['A' => 20, 'B' => 30, 'C' => 20, 'D' => 50, 'E' => 10, 'F' => 10, 'G' => 20, 'H' => 20, 'I' => 20, 'J' => 20, 'K' => 20, 'L' => 20, 'M' => 10, 'N' => 10, 'O' => 10, 'P' => 10, 'Q' => 25, 'R' => 25, 'S' => 25];
        $excelObj->setWidth($widths);
        //默认单元格居中
        $excelObj->setDefaultStyleAlignment();

        foreach ($header as $key => $value) {
            //设置字体
            $excelObj->setFrontName($letter[$key]);
            //设置加粗
            $excelObj->setFrontBold($letter[$key]);
            //设置大小
            $excelObj->setFrontSize($letter[$key]);
        }

        $excelObj->create($downList, $header);
        return true;
    }
}